$(function () {
    // 切换tab栏
    $(".tab2-content").on("click", "li", function () {
        $("#load").css("display", "none");
        $(this).addClass("active").siblings().removeClass("active");
        var text = $(this).text();
        if (text === "业主单位") {
            branchType = "0";
            $("#chart").hide();
        } else if (text === "审批部门") {
            branchType = "1";
            $("#chart").hide();
        } else if (text === "督查组") {
            branchType = "4";
            $("#chart").show();
            loadChart();
        }
        num = 0;
        page = 1;
        $(".containers").empty();
        loadContent();
    });

    $(".tab3-content").on("click", "li", function () {
        $("#load").css("display", "none");
        $(this).addClass("active").siblings().removeClass("active");
        var text = $(this).text();
        page = 1; //页数
        num = 1; //序号
        if (text === "个人排名") {
            departRank = false;
        } else if (text === "组内排名") {
            departRank = true;
        }

        num = 0;
        $(".containers").empty();
        loadContent();
    });


    // 显示模态框
    $(".top-navigation .search").click(function () {
        $(".bottom-modal").fadeIn();
        $(".fades").fadeIn();
    });

    // 关闭模态框
    $(".fades").click(function () {
        closeModal();
    });

    // 关闭模态框function
    function closeModal() {
        $(".bottom-modal").fadeOut();
        $(".fades").fadeOut();
    }
    // 重置form表单
    $(".clearForm").click(function () {
        $(".forms").find('input[type=text],select,input[type=hidden]').each(function () {
            $(this).val('');

        });

    });

    // 提交表单
    $("#search-btn").click(function () {
        var data = $(".forms").serialize();
        console.log(data);
        closeModal();
    });

    // 请求考核汇总数据
    // 获取用户电话号码
    var userPhone = window.AndroidWebView.getUserId();
    var departRank = false; //false默认为个人排名  true 为组内排名
    var branchType = "4"; //0:业主单位  1：审批部门  4：督查组
    var page = 1; //页数
    var num = 1; //序号

    // 初始化加载考核汇总
    loadContent();

    //加载内容
    function loadContent() {
        $.ajax({
            url: "http://120.78.158.120:8099/zhApp/scoreSummary/lists",
            type: "POST",
            dataType: "json", //返回数据形式为json
            contentType: 'application/json;charset=UTF-8',
            data: JSON.stringify({
                "userPhone": userPhone,
                "departRank": departRank,
                "branchType": branchType,
                "paging": {
                    "currentPage": page
                }

            }),
            success: function (res) {
                console.log(res);
                if (res.code === "200" && res.data !== undefined) {
                    $("#load").css("display", "block");

                    var item = res.data;
                    var html = "";
                    for (var i = 0; i < item.length; i++) {
                        num++
                        html = '\
                          <div class="record-item">\
                            <h3>' + num + '</h3>\
                                <div>\
                                    <div class="first-row">\
                                        <p class="title">' + item[i].checkObject + '</p>\
                                        <p class="score">' + item[i].score + '</p>\
                                    </div>\
                                    <div class="second-row">\
                                        <p class="hidden-font name">' + item[i].checkDept + '</p>\
                                        <p>排名：' + item[i].rank + '</p>\
                                    </div>\
                                </div>\
                            </div >\
                            ';
                        $(".containers").append(html);
                    }

                }

                if (res.data !== undefined) {

                    $(".load-tip h3").html("点击加载更多");
                } else {
                    $(".load-tip h3").html("数据已加载完毕");
                }
            }
        });
    }

    $(".load-tip").click(function () {
        page++
        console.log(page);
        $(".load-tip h3").html("数据正在加载中");
        loadContent();
    });

    // 请求督查组图表数据
    function loadChart() {
        $.ajax({
            url: "http://120.78.158.120:8099/zhApp/scoreSummary/lists",
            type: "POST",
            dataType: "json", //返回数据形式为json
            contentType: 'application/json;charset=UTF-8',
            data: JSON.stringify({
                "userPhone": userPhone,
                "departRank": false,
                "branchType": "4",
                "paging": {
                    "currentPage": 1
                }

            }),
            success: function (res) {
                var score = []; //分数数组
                var xname = []; //x轴name
                if (res.code === "200" && res.data !== undefined) {
                    $("#load").css("display", "block");
                    var data = res.data;
                    data.forEach(function (item) {
                        score.push(item.score);
                        xname.push(item.checkObject);
                    });
                    var obj = {
                        score: score,
                        xname: xname
                    }
                    console.log(obj);
                    chart(obj);
                }

            }
        });
    }

    loadChart();
});

function changeTba1(that, id) {
    $(".tab1-content>li").removeClass("active");
    $(that).addClass('active')
    if (id == 1) {
        $(".record-content").show(100)
        $(".score-content").hide(100)
        $(".summary-content").hide(100)
    } else if (id == 2) {
        $(".record-content").hide(100)
        $(".score-content").show(100)
        $(".summary-content").hide(100)
    } else {
        $(".record-content").hide(100)
        $(".score-content").hide(100)
        $(".summary-content").show(100)
    }
}

// 柱状图
function chart(obj) {
    var option = {
        color: ['#3398DB'],
        tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
                type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: [{
            type: 'category',
            data: obj.xname,
            axisTick: {
                show: false
            },
            axisLabel: {
                interval: 0,
                fontSize: 12,
                formatter: function (params) {
                    var newParamsName = "";
                    var paramsNameNumber = params.length;
                    var provideNumber;
                    if (paramsNameNumber <= 6) {
                        provideNumber = 3
                    } else {
                        provideNumber = 3
                    }
                    var rowNumber = Math.ceil(paramsNameNumber / provideNumber);
                    if (paramsNameNumber > provideNumber) {
                        /** 循环每一行,p表示行 */
                        for (var p = 0; p < rowNumber; p++) {
                            var tempStr = "";
                            var start = p * provideNumber;
                            var end = start + provideNumber;
                            if (p == rowNumber - 1) {
                                // 最后一次不换行
                                tempStr = params.substring(start, paramsNameNumber);
                            } else {
                                tempStr = params.substring(start, end) + "\n";
                            }
                            newParamsName += tempStr;
                        }
                    } else {
                        newParamsName = params;
                    }
                    return newParamsName
                },
            }
        }],
        yAxis: [{
            type: 'value',
            name: '分',
        }],
        series: [{
            name: '',
            animationDuration: 5000,
            type: 'bar',
            barWidth: '60%',
            data: obj.score
        }]
    };
    //基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('chart'));
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
    //自适应大小
    window.addEventListener("resize", function () {
        myChart.resize();
    });
}